
import 'package:eshi/app/SettingsPage.dart';
import 'package:eshi/app/shi/data/System.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// 应用配置 
class Config{ 
  SharedPreferences? prefs;
  static final Config _instance = Config();

  
  static SharedPreferences? getInstance(){
    return _instance.prefs;
  }

  static initSaveEngine() async {
      _instance.prefs = await SharedPreferences.getInstance();
  }

  static commonSet(k,v){
    if(v==null){
      _instance.prefs?.remove(k);
    }else{
      _instance.prefs?.setString(k,v);
    }
  }
  //最后打开的历史页
  static get lastHistory =>   _instance.prefs?.getString("lastHistory") ?? "中国历史";
  static set lastHistory(v) => commonSet("lastHistory", v);

  //服务器
  static get netHost => _instance.prefs?.getString("netHost")??"";
  static set netHost(v) => commonSet("netHost",v);

  /// assets目录是否初始化
  static get initAssets => _instance.prefs?.getBool("initAssetsed") ?? false;
  static set initAssets(v) => _instance.prefs?.setBool("initAssetsed",v);
  




  //设备别名  
  static get myName => _instance.prefs?.getString("myName");
  static set myName(v) => commonSet("myName",v);
  
  //绑定的的账号
  static get bindUsers => _instance.prefs?.getString("bindUsers") ?? "暂未绑定用户，绑定之后用于同账号下的数据同步";
  static set bindUsers(v) => commonSet("bindUsers",v);
  
  //设备名称  
  static get deviceName => _instance.prefs?.getString("deviceName") ?? System.deviceId;
  static set deviceName(v) => commonSet("deviceName",v);

  //绑定的设备  
  static get bindDevices => _instance.prefs?.getString("bindDevices");
  static set bindDevices(v) => commonSet("bindDevices",v);
  
  //语言设置 
  static String get language => _instance.prefs?.getString("language") ?? "zh_CN";
  static  get languageLocale => Language.fromCode(language).locale;
  static set language(v) => commonSet("language",v);
  
  
  //深色模式 
  static bool  isDarkMode = false;
  // static bool get isDarkMode => _instance.prefs?.getBool("isDarkMode") ?? false;
  // static set isDarkMode(v) => _instance.prefs?.setBool("isDarkMode",v);

  //历史数据项配置 
  static List<String> get historyReadOnlyList => _instance.prefs?.getStringList("historyDataConfigs") ?? [];
  static  set historyReadOnlyList(v) => _instance.prefs?.setStringList("historyDataConfigs",v);
  static List<String> get historyDisableList => _instance.prefs?.getStringList("historyDisableList") ?? ["数学史","科学史","人类史","世界史"];
  static  set historyDisableList(v) => _instance.prefs?.setStringList("historyDisableList",v);

  //编辑页自动换行开关 
  static bool get editPageisWrapText => _instance.prefs?.getBool("editPageisWrapText") ?? true;
  static set editPageisWrapText(v) => _instance.prefs?.setBool("editPageisWrapText",v);
}